import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/pages/brand_page/detail_page/barand_detail.dart';

import 'action.dart';
import 'state.dart';

////showDialog(对showGeneralDialog封裝),showGeneralDialog弹出弹窗组件，Navigator.of(context).pop(),关闭弹窗
Widget buildView(BrandState state, Dispatch dispatch, ViewService viewService) {
   String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  return
  //   Scrollbar( // 显示进度条
  //   child: SingleChildScrollView(
  //     scrollDirection:Axis.horizontal,
  //     padding: EdgeInsets.all(16.0),
  //     child: Center(
  //       child: Row(
  //         //动态创建一个List<Widget>
  //         children: str.split("")
  //         //每一个字母都用一个Text显示,字体为原来的两倍
  //             .map((c) => Text(c, textScaleFactor: 2.0,))
  //             .toList(),
  //       ),
  //     ),
  //   ),
  // );
    Container(
    alignment: Alignment.center,
    child: //点击该按钮后弹出对话框
        RaisedButton(
      child: Text("对话框1"),
      onPressed: () async {
        //弹出对话框并等待其关闭
        //   Future<void> delete =
        Navigator.pushNamed(viewService.context, 'custom_page');

        //Navigator.push(viewService.context, MaterialPageRoute(builder:(context)=>CustomPage()));
       // showDeleteConfirmDialog1(state, dispatch, viewService);
        // if (delete == null) {
        //   print("取消删除");
        // } else {
        //   print("已确认删除");
        //   //... 删除文件
        // }
      },
    ),
  );
}

// 弹出对话框
Future<bool> showDeleteConfirmDialog1(
    BrandState state, Dispatch dispatch, ViewService viewService) {
  return showDialog<bool>(
    context: viewService.context,
    builder: (context) {
      return AlertDialog(
        title: Text("提示"),
        content: Text("您确定要删除当前文件吗?"),
        actions: <Widget>[
          FlatButton(
            child: Text("取消"),
            onPressed: () => Navigator.of(context).pop(), // 关闭对话框
          ),
          FlatButton(
            child: Text("删除"),
            onPressed: () {
              //关闭对话框并返回true
              Navigator.of(context).pop(true);
            },
          ),
        ],
      );
    },
  );
}


Future<void> changeLanguage(
    BrandState state, Dispatch dispatch, ViewService viewService) async {
  int i = await showDialog<int>(
      context: viewService.context,
      builder: (BuildContext context) {
        return SimpleDialog(
          title: const Text('请选择语言'),
          children: <Widget>[
            SimpleDialogOption(
              onPressed: () {
                // 返回1
                Navigator.pop(context, 1);
              },
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 6),
                child: const Text('中文简体'),
              ),
            ),
            SimpleDialogOption(
              onPressed: () {
                // 返回2
                Navigator.pop(context, 2);
              },
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 6),
                child: const Text('美国英语'),
              ),
            ),
          ],
        );
      });
  if (i != null) {
    print("选择了：${i == 1 ? "中文简体" : "美国英语"}");
  }
}

Future<void> showListDialog(
    BrandState state, Dispatch dispatch, ViewService viewService) async {
  int index = await showDialog<int>(
    context: viewService.context,
    builder: (BuildContext context) {
      var child = Column(
        children: <Widget>[
          ListTile(
            title: Text("请选择"),
          ),
          Expanded(
            child: ListView.builder(
              itemCount: 30,
              itemBuilder: (BuildContext context, int index) {
                return ListTile(
                  title: Text("$index"),
                  onTap: () => Navigator.of(context).pop(index),
                );
              },
            ),
          ),
        ],
      );
      //使用AlertDialog会报错
      //return AlertDialog(content: child);
      // return Dialog(child: child);
      return UnconstrainedBox(
        constrainedAxis: Axis.vertical,
        child: ConstrainedBox(
          constraints: BoxConstraints(maxWidth: 280),
          child: Material(
            child: child,
            type: MaterialType.card,
          ),
        ),
      );
    },
  );
  if (index != null) {
    print("点击了：$index");
  }
}

Future<bool> showCustomDialog1(
    BrandState state, Dispatch dispatch, ViewService viewService) {
  return showCustomDialog<bool>(
    context: viewService.context,
    builder: (context) {
      return AlertDialog(
        title: Text("提示"),
        content: Text("您确定要删除当前文件吗?"),
        actions: <Widget>[
          FlatButton(
            child: Text("取消"),
            onPressed: () => Navigator.of(context).pop(),
          ),
          FlatButton(
            child: Text("删除"),
            onPressed: () {
              // 执行删除操作
              Navigator.of(context).pop(true);
            },
          ),
        ],
      );
    },
  );
}

Future<T> showCustomDialog<T>({
  @required BuildContext context,
  bool barrierDismissible = true, //点击遮罩是否关闭对话框
  WidgetBuilder builder,
}) {
  final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
  return showGeneralDialog(
    context: context,
    pageBuilder: (BuildContext buildContext, Animation<double> animation,
        Animation<double> secondaryAnimation) {
      final Widget pageChild = Builder(builder: builder);
      return SafeArea(
        child: Builder(builder: (BuildContext context) {
          return theme != null
              ? Theme(data: theme, child: pageChild)
              : pageChild;
        }),
      );
    },
    barrierDismissible: barrierDismissible,
    barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
    barrierColor: Colors.black87,
    // 自定义遮罩颜色
    transitionDuration: const Duration(milliseconds: 150),
    transitionBuilder: _buildMaterialDialogTransitions,
  );
}

Widget _buildMaterialDialogTransitions(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child) {
  // 使用缩放动画
  return ScaleTransition(
    scale: CurvedAnimation(
      parent: animation,
      curve: Curves.easeOut,
    ),
    child: child,
  );
}
